# -*- perl -*-
#  Copyright (C) 1999-2020 Yves Renard
#
#  This file is a part of GetFEM++
#
#  GetFEM++  is  free software;  you  can  redistribute  it  and/or modify it
#  under  the  terms  of the  GNU  Lesser General Public License as published
#  by  the  Free Software Foundation;  either version 3 of the License,  or
#  (at your option) any later version along with the GCC Runtime Library
#  Exception either version 3.1 or (at your option) any later version.
#  This program  is  distributed  in  the  hope  that it will be useful,  but
#  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
#  or  FITNESS  FOR  A PARTICULAR PURPOSE.  See the GNU Lesser General Public
#  License and GCC Runtime Library Exception for more details.
#  You  should  have received a copy of the GNU Lesser General Public License
#  along  with  this program;  if not, write to the Free Software Foundation,
#  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.


eval 'exec perl -S $0 "$@"'
  if 0;

$prefix = "inte";
$count = int(1000 * rand);
if ($ENV{TMPDIR} eq "") { $tmpdir = "/tmp"; } else { $tmpdir = $ENV{TMPDIR} }
if (substr($tmpdir, length($tmpdir)-1, 1) eq "/")
  { $tmpdir = substr($tmpdir, 0, length($tmpdir)-1); }
do { $tmp1 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp1);
do { $tmp2 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp2);
do { $tmp3 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp3);
do { $tmp4 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp4);
do { $tmp5 = $tmpdir."/".$prefix."\_$count"; ++$count; } while (-f $tmp5);

sub catch { `rm -f $tmp1 $tmp2 $tmp3 $tmp4 $tmp5`; }
$SIG{INT} = 'catch';

sub read_line {
  while(1) {
    $li = <DATAF>; if (!($li)) { return; }
    chomp($li); ($li, $a) = split('\%', $li, 2);
    $li =~s/\s//g;
    if ($li) { return; }
  }
}

open(TMPF1, ">$tmp1") or die "Open file impossible : $!\n";
open(TMPF2, ">$tmp2") or die "Open file impossible : $!\n";
open(TMPF3, ">$tmp3") or die "Open file impossible : $!\n";
open(TMPF4, ">$tmp4") or die "Open file impossible : $!\n";
open(TMPF5, ">$tmp5") or die "Open file impossible : $!\n";

print TMPF1 "  static im_desc im_desc_tab[NB_IM] = {\n";
print TMPF3 "  static const char * im_desc_real[NB_IMR] = {\n";
print TMPF4 "  static const char * im_desc_face_meth[NB_IMF] = {\n";
print TMPF5 "  static size_type im_desc_node_type[NB_IMN] = {\n";

$lss = `ls $ARGV[0]/*.IM`;
$nb_methods = 0;
$nb_faces = 0;
$nb_real = 0;
$nb_int = 0;

while ($lss) {

  ($filename, $lss) = split('\n', $lss, 2);

  open(DATAF, $filename) or die "Open file impossible : $!\n";
  read_line;
  if (!($li=~/NAME=/))
    { die "Impossible to read $filename : no NAME found\n"; }
  ($a, $name) = split('=', $li, 2);
  $name = `echo "$name" | tr [a-z] [A-Z]`;
  print "Reading $name";
  read_line;
  if (!($li=~/N=/)) { die "Impossible to read $filename : no N found\n"; }
  ($a, $N) = split('=', $li, 2); $N *= 1;
  read_line;
  if (!($li=~/GEOTRANS=/))
    { die "Impossible to read $filename : no GEOTRANS found\n"; }
  ($a, $geotrans) = split('=', $li, 2); $N *= 1;
  read_line;
  if (!($li=~/NBPT=/))
    { die "Impossible to read $filename : no NBPT found\n"; }
  ($a, $nbpt) = split('=', $li, 2); $nbpt *= 1;
  if ($nbpt) { print TMPF5 "    "; print TMPF3 "    // $name\n"; }
  for ($i = 0; $i < $nbpt; ++$i) {
    read_line;
    if (!($li=~/,/))
      { die "Impossible to read $filename : integration node unreadable\n"; }
    ($a, $b) = split(',', $li, 2);
    print TMPF5 $a, ", ";
    for ($j = 0; $j < $N; ++$j) {
      if (!($b=~/,/))
	{ die "Impossible to read $filename : wrong number of coordinates\n"; }
      ($a, $b) = split(',', $b, 2);
      # print TMPF3 " LONG_SCAL(", $a, "),\n";
      print TMPF3 "  \"", $a, "\",\n";
    }
    if (!($b))
      { die "Impossible to read $filename : wrong number of coordinates\n"; }
    print TMPF3 "  \"", $b, "\",\n";
  }
  if ($nbpt) { print TMPF5 " // $name\n"; }
  read_line;
  if (!($li=~/NBF=/))
    { die "Impossible to read $filename : no NBF found\n"; }
  ($a, $nbf) = split('=', $li, 2); $nbf *= 1;
  if ($nbf) { print TMPF4 "    // $name\n    "; }
  for ($i = 0; $i < $nbf; ++$i) {
    read_line;
    if (!($li=~/IM_/)) { $li = "IM_".$li; }
    print TMPF4 "\"$li\",";
  }
  if ($nbf) { print TMPF4 "\n"; }
  close(DATAF);
  $nb_methods++;
  chop($name);
  print TMPF1 "    {\"$name\", \"$geotrans\", $nbpt, $nb_real, $nb_faces, $nb_int},\n";
  $nb_faces += $nbf;
  $nb_real += $nbpt * ($N + 1);
  $nb_int += $nbpt;
}

$year = `date +%Y`;
chomp $year;

print TMPF2 "// This file is generated by make_getfem_list\n";
print TMPF2 "
/*===========================================================================

 Copyright (C) 2002-$year Yves Renard

 This file is a part of GetFEM++

 GetFEM++  is  free software;  you  can  redistribute  it  and/or modify it
 under  the  terms  of the  GNU  Lesser General Public License as published
 by  the  Free Software Foundation;  either version 3 of the License,  or
 (at your option) any later version along with the GCC Runtime Library
 Exception either version 3.1 or (at your option) any later version.
 This program  is  distributed  in  the  hope  that it will be useful,  but
 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 or  FITNESS  FOR  A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 License and GCC Runtime Library Exception for more details.
 You  should  have received a copy of the GNU Lesser General Public License
 along  with  this program;  if not, write to the Free Software Foundation,
 Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.

 As a special exception, you  may use  this file  as it is a part of a free
 software  library  without  restriction.  Specifically,  if   other  files
 instantiate  templates  or  use macros or inline functions from this file,
 or  you compile this  file  and  link  it  with other files  to produce an
 executable, this file  does  not  by itself cause the resulting executable
 to be covered  by the GNU Lesser General Public License.  This   exception
 does not  however  invalidate  any  other  reasons why the executable file
 might be covered by the GNU Lesser General Public License.

===========================================================================*/

/**\\file getfem_im_list.h
   \\brief  This file is generated by cubature/make_getfem_list */
\n\n
namespace getfem {\n\n
  struct im_desc {
      const char *method_name;
      const char *geotrans_name;
      size_type nb_points;
      size_type firstreal;
      size_type firstface;
      size_type firsttype;
  };\n\n
  static const int NB_IM=$nb_methods;\n\n";
print TMPF1 "  };\n\n  static const int NB_IMR=$nb_real; \n\n";
print TMPF3 "  };\n\n  static const int NB_IMF=$nb_faces; \n\n";
print TMPF4 "  };\n\n  static const int NB_IMN=$nb_int; \n\n";
print TMPF5 "  };\n\n}\n\n";

close(TMPF1); close(TMPF2); close(TMPF3); close(TMPF4); close(TMPF5);

`cat $tmp2 $tmp1 $tmp3 $tmp4 $tmp5 > getfem_im_list.h`;
`rm -f $tmp1 $tmp2 $tmp3 $tmp4 $tmp5`;

print "The result is in getfem_im_list.h\n";
